package com.blueheart.music.controller;

import com.blueheart.music.common.ResultUtil;
import com.blueheart.music.consts.MyConst;
import com.blueheart.music.exception.BaseExceptionEnum;
import com.blueheart.music.model.pojo.ListSong;
import com.blueheart.music.service.ListSongService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
public class ListSongController {
    @Autowired
    private ListSongService listSongService;

    /**
     * 添加歌单中的歌曲
     * @param request
     * @return
     */
    @PostMapping("/listSong/addListSong")
    public ResultUtil addListSong(HttpServletRequest request){
        String songId = request.getParameter(MyConst.SONGID).trim();
        String songListId = request.getParameter(MyConst.SONGLISTID).trim();
        if (StringUtils.isEmpty(songId)||StringUtils.isEmpty(songListId)){
            return ResultUtil.error(BaseExceptionEnum.PARAM_ERROR);
        }
        ListSong listSong = new ListSong();
        listSong.setSongId(new Integer(songId));
        listSong.setSongListId(new Integer(songListId));
        Boolean flag = listSongService.addListSong(listSong);
        if (flag){
            return ResultUtil.success();
        }
        return ResultUtil.error(BaseExceptionEnum.ADD_ERROR);
    }

    /**
     * 删除歌单中的歌曲
     * @param id
     * @return
     */
    @PostMapping("/listSong/deleteListSong")
    public ResultUtil deleteListSong(@RequestParam("id") Integer id){
        if (id>0){
            Boolean flag = listSongService.removeListSong(id);
            if (flag){
                return ResultUtil.success();
            }
        }
        return ResultUtil.error(BaseExceptionEnum.PARAM_ERROR);
    }

    /**
     * 更新歌单中的歌曲
     * @param request
     * @return
     */
    @PostMapping("/listSong/updateListSong")
    public ResultUtil updateListSong(HttpServletRequest request){
        String id = request.getParameter(MyConst.SING_ID).trim();
        String songId = request.getParameter(MyConst.SONGID).trim();
        String songListId = request.getParameter(MyConst.SONGLISTID).trim();
        if (StringUtils.isEmpty(id)||StringUtils.isEmpty(songId)||StringUtils.isEmpty(songListId)){
            return ResultUtil.error(BaseExceptionEnum.PARAM_ERROR);
        }
        ListSong listSong = new ListSong();
        listSong.setId(new Integer(id));
        listSong.setSongId(new Integer(songId));
        listSong.setSongListId(new Integer(songListId));
        Boolean flag = listSongService.reviseListSong(listSong);
        if (flag){
            return ResultUtil.success();
        }
        return ResultUtil.error(BaseExceptionEnum.UPDATE_ERROR);
    }

    /**
     * 查询全部
     * @return
     */
    @GetMapping("/listSong/findAll")
    public ResultUtil findALL(){
        List<ListSong> all = listSongService.findAll();
        if (all.size()>0){
            return ResultUtil.success(all);
        }
        return ResultUtil.error(BaseExceptionEnum.SELETE_ERROR);
    }

    /**
     * 根据主键查询
     * @param id
     * @return
     */
    @PostMapping("/listSong/findById")
    public ResultUtil findListSongById(@RequestParam("id") Integer id){
        if (id>0){
            ListSong byId = listSongService.findById(id);
            if (byId!=null){
                return  ResultUtil.success(byId);
            }
        }
        return ResultUtil.error(BaseExceptionEnum.SELETE_ERROR);
    }

    /**
     * 根据歌手id查询
     * @param songId
     * @return
     */
    @PostMapping("/listSong/findBySongId")
    public ResultUtil findBySongId(@RequestParam("songId") Integer songId){
        if (songId>0){
            List<ListSong> bySongId = listSongService.findBySongId(songId);
            if (bySongId.size()>0){
                return ResultUtil.success(bySongId);
            }
        }
        return ResultUtil.error(BaseExceptionEnum.SELETE_ERROR);
    }

    /**
     * 通过歌单id查询
     * @param songListId
     * @return
     */
    @PostMapping("/listSong/findBySongListId")
    public ResultUtil findBySongListId(@RequestParam("songListId") Integer songListId){
        if (songListId>0){
            List<ListSong> bySongListId = listSongService.findBySongListId(songListId);
            if (bySongListId.size()>0){
                return ResultUtil.success(bySongListId);
            }
        }
        return ResultUtil.error(BaseExceptionEnum.SELETE_ERROR);
    }
}
